package com.liaogui.strategy;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author liaogui
 * @date 2025/2/3 21:32
 */
@Component
public class StrategyUseService implements ApplicationContextAware {
    private Map<FileTypeResolveEnum, IFileStrategy> iFileStrategyMap = new ConcurrentHashMap<>();

    public void resolveFile(FileTypeResolveEnum fileTypeResolveEnum, Object objectParam) {
        IFileStrategy iFileStrategy = iFileStrategyMap.get(fileTypeResolveEnum);
        if (null != iFileStrategy) {
            iFileStrategy.resolve(objectParam);
        }
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, IFileStrategy> beans = applicationContext.getBeansOfType(IFileStrategy.class);
        beans.values().forEach(iFileStrategy -> iFileStrategyMap.put(iFileStrategy.gainFileType(), iFileStrategy));
    }
}
